<?php
/**
Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

/** to navigate to some other url */
function funct_go2($args, $env)
{
	//just empty right now..
	$url = $args[0];
	$url = str_replace(".", "/", $url);
	$url = "../".$url."_html.php";
	
	$user_type = $env["user_type"];
	$user_token = $env["user_token"];
	
	$params = "sy_user_type=".$user_type."&".
				"sy_user_token=".$user_token."&";
	
	for($i = 1; $i < count($args); $i += 2)
	{
		$key = $args[$i];
		$value = $args[$i + 1];
		
		$params .= "pg_".$key."=".$value."&";
	}
	$url.= "?".$params;
	$env["redirect_to"] = $url;
	if($env["intergrated"]) //then just set the header without any fear..
	{
		header("Location: http://" . $_SERVER['HTTP_HOST']
                     . dirname($_SERVER['PHP_SELF'])
                     . "/" . $url); /* Redirect browser */
	}
	return array(NULL, $env);
}

function funct_refresh($args, $env)
{
	$timeout= $args[0];
	$env["refresh_timout"] = $timeout;
	
	return array(NULL, $env);
}

function funct_count($args, $env)
{
	$data = $args[0];
	return array(count($data), $env);
}

function funct_update($args, $env)
{
	return array(NULL, $env);
}

function funct_get_today($args, $env)
{
	$today = getdate();
	$ret_val = $today['year']."-".
			$today['mon']."-".
			$today['mday'];
			
	return array($ret_val, $env);
}

function funct_new_date($args, $env)
{
	if(!is_array($args) || ($args[0] == 0 || $args[0] == "" ||
		$args[1] == 0 || $args[1] == "" ||
		$args[2] == 0 || $args[2] == "" ))
	{
		$ret_val = "";
	}
	else
	{
		$ret_val = $args[0]."-".
			$args[1]."-".
			$args[2];
	}
			
	return array($ret_val, $env);
}

//handling dates..
function funct_get_year($args, $env)
{
	$date = $args[0];
	//some poor way of handling date..
	$pos = strpos($date, "-");
	if($pos !== false)
	{
		$ret_val = substr($date,0, $pos);
		return array($ret_val, $env);
	}
	return array("", $env);;
}

function funct_get_month($args, $env)
{
	//some poor way of handling date..
	$date = $args[0];
	$pos = strpos($date, "-");
	if($pos !== false)
	{
		$month_day = substr($date, $pos+1);
		$pos = strpos($month_day, "-");
		if($pos !== false)
		{
			$ret_val = substr($month_day,0, $pos);
			return array($ret_val, $env);
		}
		return array("", $env);;
	}
	return array("", $env);
}

function funct_get_day($args, $env)
{
	//some poor way of handling date..
	$date = $args[0];
	$pos = strpos($date, "-");
	if($pos !== false)
	{
		$month_day = substr($date, $pos+1);
		$pos = strpos($month_day, "-");
		if($pos !== false)
		{
			$ret_val = substr($month_day,$pos + 1);
			return array($ret_val, $env);
		}
		return array("", $env);;
	}
	return array("", $env);
}

//handling time..
function funct_get_hours($args, $env)
{
	$time = $args[0];
	//some poor way of handling date..
	$pos = strpos($time, ":");
	if($pos !== false)
	{
		$ret_val = substr($time,0, $pos);
		return array($ret_val, $env);
	}
	return array("", $env);;
}

function funct_get_mins($args, $env)
{
	//some poor way of handling date..
	$time = $args[0];
	$pos = strpos($time, ":");
	if($pos !== false)
	{
		$mins_secs = substr($time, $pos+1);
		$pos = strpos($mins_secs, ":");
		if($pos !== false)
		{
			$ret_val = substr($mins_secs,0, $pos);
			return array($ret_val, $env);
		}
		return array("", $env);;
	}
	return array("", $env);
}

function funct_get_secs($args, $env)
{
	//some poor way of handling date..
	$time = $args[0];
	$pos = strpos($time, ":");
	if($pos !== false)
	{
		$mins_secs = substr($time, $pos+1);
		$pos = strpos($mins_secs, ":");
		if($pos !== false)
		{
			$ret_val = substr($mins_secs,$pos + 1);
			return array($ret_val, $env);
		}
		return array("", $env);;
	}
	return array("", $env);
}

// setting year..
function funct_set_year($args, $env)
{
	$date = $args[0];
	$num = $args[1];
	$pos = strpos($date, "-");
	if($pos !== false)
	{
		$year = substr($date, 0, $pos);
		$month_day = substr($date, $pos+1);
		$pos = strpos($month_day, "-");
		if($pos !== false)
		{
			$month = substr($month_day, 0, $pos);
			$day = substr($month_day,$pos + 1);
			$ret_val = $num."-".
					$month."-".
					$day; //num is replaced..
			return array($ret_val, $env);
		}
		$ret_val = $date;
		return array($ret_val, $env);
	}
	$ret_val = $date;
	return array($ret_val, $env);	
}

function get_month_short_name($num)
{
	$month_map = array("Jan", "Feb", "Mar", "Apr",
				  	"May", "Jun", "Jul", "Aug",
				  	"Sep", "Oct", "Nov", "Dec");
	return $month_map[$num];
}

function get_month_number($month)
{
	$month_map = array("Jan"=>1, "Feb"=>2, "Mar"=>3, "Apr"=>4,
				  	"May"=>5, "Jun"=>6, "Jul"=>7, "Aug"=>8,
				  	"Sep"=>9, "Oct"=>10, "Nov"=>11, "Dec"=>12);
	return $month_map[$month];
}

function funct_set_month($args, $env)
{
	$date = $args[0];
	$new_month = $args[1];
	
	if(!ctype_digit($new_month))
	{
		$new_month = get_month_number($new_month);
	}
	
	$pos = strpos($date, "-");
	if($pos !== false)
	{
		$year = substr($date, 0, $pos);
		$month_day = substr($date, $pos+1);
		$pos = strpos($month_day, "-");
		if($pos !== false)
		{
			$month = substr($month_day, 0, $pos);
			$day = substr($month_day,$pos + 1);
			$ret_val = $year."-".
					$new_month."-".
					$day; //num is replaced..
			return array($ret_val, $env);
		}
		$ret_val = $date;
		return array($ret_val, $env);
	}
	$ret_val = $date;
	return array($ret_val, $env);
}

function funct_set_day($args, $env)
{
	$date = $args[0];
	$new_day = $args[1];
	
	$pos = strpos($date, "-");
	if($pos !== false)
	{
		$year = substr($date, 0, $pos);
		$month_day = substr($date, $pos+1);
		$pos = strpos($month_day, "-");
		if($pos !== false)
		{
			$month = substr($month_day, 0, $pos);
			$day = substr($month_day,$pos + 1);
			$ret_val = $year."-".
					$month."-".
					$new_day; //num is replaced..
			return array($ret_val, $env);
		}
		$ret_val = $date;
		return array($ret_val, $env);
	}
	$ret_val = $date;
	return array($ret_val, $env);
}

//function to set time attributes..

function funct_set_hours($args, $env)
{
	$time = $args[0];
	$num = $args[1];
	$pos = strpos($time, ":");
	if($pos !== false)
	{
		$hours = substr($time, 0, $pos);
		$min_secs = substr($time, $pos+1);
		$pos = strpos($min_secs, ":");
		if($pos !== false)
		{
			$mins = substr($min_secs, 0, $pos);
			$secs = substr($min_secs ,$pos + 1);
			$ret_val = $num.":".
					$mins.":".
					$secs; //num is replaced..
			return array($ret_val, $env);
		}
		$ret_val = $time;
		return array($ret_val, $env);
	}
	$ret_val = $time;
	return array($ret_val, $env);	
}

function funct_set_mins($args, $env)
{
	$time = $args[0];
	$new_mins = $args[1];

	$pos = strpos($time, ":");
	if($pos !== false)
	{
		$hours = substr($time, 0, $pos);
		$min_secs = substr($time, $pos+1);
		$pos = strpos($min_secs, ":");
		if($pos !== false)
		{
			$mins = substr($min_secs, 0, $pos);
			$secs = substr($min_secs ,$pos + 1);
			$ret_val = $hours.":".
					$new_mins.":".
					$secs; //num is replaced..
			return array($ret_val, $env);
		}
		$ret_val = $time;
		return array($ret_val, $env);
	}
	$ret_val = $time;
	return array($ret_val, $env);
}

function funct_set_secs($args, $env)
{
	$time = $args[0];
	$new_secs = $args[1];
	
	$pos = strpos($time, ":");
	if($pos !== false)
	{
		$hours = substr($time, 0, $pos);
		$min_secs = substr($time, $pos+1);
		$pos = strpos($min_secs, ":");
		if($pos !== false)
		{
			$mins = substr($min_secs, 0, $pos);
			$secs = substr($min_secs ,$pos + 1);
			$ret_val = $hours.":".
					$mins.":".
					$new_secs; //num is replaced..
			return array($ret_val, $env);
		}
		$ret_val = $time;
		return array($ret_val, $env);
	}
	$ret_val = $time;
	return array($ret_val, $env);
}


function funct_compare_dates($args, $env)
{
	$date1 = $args[0];
	$date2 = $args[1];
	
	$day1 = $month1 = $year1 = $day2 = $month2 = $year2 = 0;
	$pos = strpos($date1, "-");
	if($pos !== false)
	{
		$year1 = substr($date1, 0, $pos);
		$month_day1 = substr($date1, $pos+1);
		$pos = strpos($month_day1, "-");
		if($pos !== false)
		{
			$month1 = substr($month_day1, 0, $pos);
			$day1 = substr($month_day1, $pos + 1);
		}
	}
	
	$pos = strpos($date2, "-");
	if($pos !== false)
	{
		$year2 = substr($date2, 0, $pos);
		$month_day2 = substr($date2, $pos+1);
		$pos = strpos($month_day2, "-");
		if($pos !== false)
		{
			$month2 = substr($month_day2, 0, $pos);
			$day2 = substr($month_day2, $pos + 1);
		}
	}
	
	if($year1 > $year2)
	{
		return array(1, $env);
	}
	elseif($year1 < $year2)
	{
		return array(-1, $env);
	}
	elseif($year1 == $year2)
	{
		if($month1 > $month2)
		{
			return array(1, $env);
		}
		elseif($month1 < $month2)
		{
			return array(-1, $env);
		}
		elseif($month1 ==  $month2)
		{
			if($day1 > $day2)
			{
				return array(1, $env);
			}
			elseif($day1 < $day2)
			{
				return array(-1, $env);
			}
			elseif($day1 == $day2)
			{
				return array(0, $env);
			}
		}

	}
}


function funct_compare_time($args, $env)
{
	$time1 = $args[0];
	$time2 = $args[1];
	
	$hours1 = $mins1 = $secs1 = $hours2 = $mins2 = $secs2 = 0;
	$pos = strpos($time1, ":");
	if($pos !== false)
	{
		$hours1 = substr($time1, 0, $pos);
		$mins_secs1 = substr($time1, $pos+1);
		$pos = strpos($mins_secs1, ":");
		if($pos !== false)
		{
			$mins1 = substr($mins_secs1, 0, $pos);
			$secs1 = substr($mins_secs1, $pos + 1);
		}
	}
	
	$pos = strpos($time2, ":");
	if($pos !== false)
	{
		$hours2 = substr($time2, 0, $pos);
		$mins_secs2 = substr($time2, $pos+1);
		$pos = strpos($mins_secs2, ":");
		if($pos !== false)
		{
			$mins2 = substr($mins_secs2, 0, $pos);
			$secs2 = substr($mins_secs2, $pos + 1);
		}
	}
	
	if($hours1 > $hours2)
	{
		return array(1, $env);
	}
	elseif($hours1 < $hours2)
	{
		return array(-1, $env);
	}
	elseif($hours1 == $hours1)
	{
		if($mins1 > $mins2)
		{
			return array(1, $env);
		}
		elseif($mins1 < $mins2)
		{
			return array(-1, $env);
		}
		elseif($mins1 ==  $mins2)
		{
			if($secs1 > $secs2)
			{
				return array(1, $env);
			}
			elseif($secs1 < $secs2)
			{
				return array(-1, $env);
			}
			elseif($secs1 == $secs2)
			{
				return array(0, $env);
			}
		}
	}
}

function funct_login($args, $env)
{
	$user_name = $args[0];
	$user_password = $args[1];
	
	$link = $env["link"];
	$sys_module_xml = "../System/module.xml";
	
	//load the table names User inheirted..
	$user_tables = array();
	$doc = new DOMDocument();
	$doc->load($sys_module_xml);
	
	$child_nodes = $doc->documentElement->childNodes;
	foreach($child_nodes as $child_node)
	{
		if($child_node->nodeType != XML_ELEMENT_NODE)
		{
			continue;
		}
		if($child_node->getAttribute("name") == "User") //get the default users
		{
			$child_tables = $child_node->childNodes;
			foreach($child_tables as $child_table)
			{
				if($child_table->nodeType != XML_ELEMENT_NODE)
				{
					continue;
				}
				if($child_table->tagName == "child")
				{
					if($child_table->firstChild)
					{
						 $table_name = trim($child_table->firstChild->nodeValue);
						 $table_name = str_replace( ".", "_", $table_name);
						 $user_tables[count($user_tables)] = $table_name;
					}
				}
			}
		}
	}
	
	
	$found = false;
	$user_type = $user_table;
	$user_id = -1;
	
	foreach($user_tables as $user_table)
	{
		$query = "SELECT id FROM $user_table WHERE ".
					"name = '". $user_name."' and ".
					"password = md5('". $user_password."')";
					
		$result = mysql_query($query) or die("Error in query: $query ".mysql_error());
		
		if($result == 0 || mysql_num_rows($result) ==  0)
		{
			continue; //it may be in another child
		}
		$row = mysql_fetch_row($result);
		
		$user_type = $user_table;
		$user_id = $row[0];
		$found = true;
		break;
	}
	if($found)
	{
		$user_token = md5($user_name.":".time().$user_password); 
		$timestamp = time();
		$query = "UPDATE $user_type SET userToken = '".$user_token."', ".
												"timeStamp = '".$timestamp. "' ".
												"WHERE id = ".$user_id;
		
		$result = mysql_query($query) or die("Error in query: $query ".mysql_error());
		$env["user_token"] = $user_token;
		$env["user_type"] = str_replace("_", ".", $user_type);
		
	}
	else
	{
		$env["user_token"] = NULL;
		$env["user_type"] = NULL;
	}
	
	$ret_val = array(); //no idea what should be here..
	return array($ret_val, $env);
}


function funct_encrypt($args, $env)
{
	$text = $args[0];
	$encrypted = md5($text);
	
	$ret_val = $encrypted;
	return array($ret_val, $env);
}

function funct_get_this_user($args, $env)
{
	$SESSION_LIMIT = 15; //in minitues..
	$user_type = $env["user_type"];
	$user_token = $env["user_token"];
	
	if(!isset($user_type) || $user_type == "" || $user_type == NULL)
	{
		return array(NULL, $env);
	}
	
	$user_type = str_replace( ".", "_", $user_type);
	
	$query = "SELECT id, timeStamp FROM $user_type WHERE ".
				"userToken = '".$user_token."'";
	
	$result = mysql_query($query) or die("Error in query: $query ".mysql_error());
	
	$ret_val = "";
	
	if($row = mysql_fetch_row($result)) 
	{
		$id = $row[0];
		$timeStamp = $row[1];
		$currentTimeStamp = time();
		
		if($currentTimeStamp - $timeStamp > $SESSION_LIMIT * 60)
		{
			//just invalidate the session..
			$ret_val = NULL;
		}
		else
		{
			//else update the currentTimeStamp..
			$query = "UPDATE System_Administrator SET ".
													"timeStamp = '".$timeStamp. "' ".
													"WHERE id = ".$id;
			
			$result = mysql_query($query) or die("Error in query: $query ".mysql_error());	
			$ret_val = $id;
		}
	}
	return array($ret_val, $env);
}

function funct_schedule($args, $env)
{
	//print_r($args);
	$start_date = $args[0];
	$start_time = $args[1];
	$end_date = $args[2];
	$end_time = $args[3];
	
	$event_values = $args[4];
	$event_var = $env["temp_params"][4]['var'];
	$event_table = $env["temp_params"][4]['table'];
	
	$param_values = array();
	$param_vars = array();
	$param_tables = array();
	
	$args_count = count($args);
	//here on all are params for the schudling..
	for($i = 5, $j = 0; $i < $args_count; $i ++, $j ++)
	{
		$param_values[$j] = $args[$i];
		$param_vars[$j] = $env["temp_params"][$i]['var'];
		$param_tables[$j] = $env["temp_params"][$i]['table'];
	}
	
	//the events suppose to have id, date, start_time, end_time attributes..
	$query = "SELECT id, date, start_time, end_time, duration FROM ".$event_table.
			" WHERE ";
	$event_ids = array_keys($event_values);
	foreach($event_ids as $event_id)
	{
		$query .= "id = ${event_id} OR ";
	}
	$query .= "0";
	
	$result = mysql_query($query) or die ("Error in query: $query ".mysql_error()); 
	if (mysql_num_rows($result) > 0) 
	{
		while($row = mysql_fetch_row($result)) 
		{
			$id = $row[0];
			$current_event_date = $row[1];
			$current_start_time = $row[2];
			$current_end_time = $row[3];
			$current_duration = $row[4];
			
			$event_values[$id]['event_date'] = $current_event_date;
			$event_values[$id]['start_time'] = $current_start_time;
			$event_values[$id]['end_time'] = $current_end_time;
			$event_values[$id]['duration'] = $current_duration;
			
			$event_values[$id]['params'] = array();
		}
	}
	
	//do the same for the schedulable parameters..
	for($i = 0; $i < count($param_values); $i ++ )
	{
		// the params have id and events fields
		// the thing is events is an array! so..
		// lets do
		$param_name = $param_vars[$i];
		
		$event_values_ids = array_keys($event_values);
		foreach($event_values_ids as $event_values_id)
		{
			$event_values[$event_values_id]['params'][$param_name] = array();
		}
		
		$query = "SELECT id, events FROM ".$param_tables[$i]."_events".
				" WHERE ";
		$param_ids = array_keys($param_values[$i]);
		foreach($param_ids as $param_id)
		{
			$query .= "id = ${param_id} OR ";
		}
		$query .= "0";
		
		$result = mysql_query($query) or die ("Error in query: $query ".mysql_error()); 
		
		if(mysql_num_rows($result) > 0) 
		{
			while($row = mysql_fetch_row($result)) 
			{
				$id = $row[0];
				$event = $row[1];
				
				if(!is_array($param_values[$i][$id]['events']))
				{
					$param_values[$i][$id]['events'] = array();
				}
				$new_index_for_event = count($param_values[$i][$id]['events']);
				$param_values[$i][$id]['events'][$new_index_for_event] = $event;
				
				$next_event_params_id = count($event_values[$event]['params'][$param_name]);
				$event_values[$event]['params'][$param_name][$next_event_params_id] = $id;
			}
		}
	}
	
	//initialize occupied array..
	$occupied_array = array();
	
	$event_ids = array_keys($event_values);
	
	foreach($event_ids as $event_id)
	{
		$event = $event_values[$event_id];
		//take out parameters..
		$event_params = $event['params'];
		$param_names = array_keys($event_params);
		foreach($param_names as $param_name)
		{
			if(!is_array($occupied_array[$param_name] ))
			{
				$occupied_array[$param_name] = array();
			}
			$param_things = $event_params[$param_name];
			$param_thing_ids = array_keys($param_things);
				//foreach thing in the list.
			foreach($param_thing_ids as $param_thing_id)
			{
				$param_thing = $param_things[$param_thing_id];
				$occupied_array[$param_name][$param_thing] = array();
			}
				
		}
	}
	
	//first fill the currently availble tracks..
	//to be implemented..
	
	//schedule each events..
	$event_ids = array_keys($event_values);
	
	foreach($event_ids as $event_id)
	{
		//first set the start time..
		$event = $event_values[$event_id];
		//take out parameters..
		$event_params = $event['params'];
		
		$duration = $event['duration'];
		
		//inital assignments..
		$current_start_date = $start_date;
		$current_start_time = $start_time;
		$current_end_time = funct_time_add($current_start_time, $duration);
		
		
		$comparison = funct_compare_time(array($current_end_time, $end_time), NULL);
		if( $comparison[0] > 0 )
		{
			return "The Event Duration ${duration} is greater than the time allowed\n";
		}
		
		$all_free_slot_found = true;
		
		
		do
		{
			$param_names = array_keys($event_params);
			foreach($param_names as $param_name)
			{
				//collect things..
				$param_things = $event_params[$param_name];
				
				$param_thing_ids = array_keys($param_things);
				//foreach thing in the list.
				foreach($param_thing_ids as $param_thing_id)
				{
					$param_thing = $param_things[$param_thing_id];
					
					
					$param_thing_free = 
						is_param_thing_free($param_name, $param_thing,
								$current_start_date, $current_start_time, $current_end_time,
								$occupied_array);
					if(!$param_thing_free["is_free"])
					{
						$current_start_date = $param_thing_free['new_date'];
						$current_start_time = $param_thing_free['new_time'];
						$current_end_time = funct_time_add($current_start_time, $duration);
						
						
						$ret_val = funct_compare_time(array($current_end_time, $end_time), NULL);
						if($ret_val[0] >= 0 )
						{
							$$all_free_slot_found = false;
							//current time exceed the natural limit..
							//go to the next date..
							$current_start_date = funct_days_add($current_start_date, 1);
							$current_start_time = $start_time;
							$current_end_time = funct_time_add($current_start_time, $duration);
							$comparison = funct_compare_time(array($current_end_time, $end_time), NULL);
							if( $comparison[0] > 0 )
							{
								return "The Event Duration ${duration} is greater than the time allowed\n";
							}
						}
					}
				}
			}
		}
		while(!$all_free_slot_found);
		
		/*
		 * so after found the
		 * current_start_time, current_end_time 
		 */
		$param_names = array_keys($event_params);
		foreach($param_names as $param_name)
		{
			//collect things..
			$param_things = $event_params[$param_name];
			
			$param_thing_ids = array_keys($param_things);
			//foreach thing in the list.
			foreach($param_thing_ids as $param_thing_id)
			{
				$param_thing = $param_things[$param_thing_id];
				
				$next_param_occupied_id = count($occupied_array[$param_name][$param_thing]);
				$occupied_array[$param_name][$param_thing][$next_param_occupied_id]
						= array("start_time"=> $current_start_time,
								"end_time"=> $current_end_time,
								"date"=> $current_start_date,
								"duration"=> $duration);
								
			}
		}
				
		$event_values[$event_id]['event_date'] = $current_start_date;
		$event_values[$event_id]['start_time'] = $current_start_time;
		$event_values[$event_id]['end_time'] = $current_end_time;
			
	}
	//print_r($occupied_array);
	/*print_r($event_values);*/
			
	$event_ids = array_keys($event_values);
	foreach($event_ids as $event_id)
	{
		$start_date = $event_values[$event_id]['event_date'];
		$start_time = $event_values[$event_id]['start_time'];
		$end_time = $event_values[$event_id]['end_time'];
		
		$query = 	"UPDATE ".$event_table." ".
					"SET date='${start_date}', start_time='${start_time}', end_time='${end_time}' ".
					"WHERE id=${event_id}";
					
		
		$result = mysql_query($query) or die ("Error in query: $query ".mysql_error()); 
		
	}
}

function funct_time_add($time, $duration)
{
	$ret = funct_get_hours(array($time), NULL);
	$hours = $ret[0];
	$ret = funct_get_mins(array($time), NULL);
	$mins = $ret[0];
	$ret = funct_get_secs(array($time), NULL);
	$secs = $ret[0];
	
	
	$ret = funct_get_hours(array($duration), NULL);
	$add_hours = $ret[0];
	$ret = funct_get_mins(array($duration), NULL);
	$add_mins = $ret[0];
	$ret = funct_get_secs(array($duration), NULL);
	$add_secs = $ret[0];
	
	//set new seconds
	$new_secs = $secs + $add_secs;
	if($new_secs >= 60)
	{
		$new_secs -= 60;
		$add_mins += 1;
	}
	
	$new_mins = $mins + $add_mins;
	if($new_mins >= 60)
	{
		$new_mins -= 60;
		$new_hours += 1;
	}
	
	$new_hours = $hours + $add_hours;
	
	return $new_hours.":".$new_mins.":".$new_secs;
}

function funct_days_add($date, $adding_days)
{
	$ret = funct_get_year(array($date), NULL);
	$year = $ret[0];
	$ret = funct_get_month(array($date), NULL);
	$month = $ret[0];
	$ret = funct_get_day(array($date), NULL);
	$day = $ret[0];
	
	$day += $adding_days;
	if($day > 31) //Assuming month has 31 days..
	{
		$day -= 31;
		$month += 1;
	}
	if($month > 12)
	{
		$month -= 12;
		$year += 1;
	}
	
	return $year."-".$month."-".$day;
}

function is_param_thing_free($param_name, $param_thing,
							$current_start_date, $current_start_time, $current_end_time,
							$occupied_array)
{
	$param_thing_timeslots = $occupied_array[$param_name][$param_thing];
	
	$final_ret_val = array();
	
	foreach($param_thing_timeslots as $param_thing_timeslot)
	{
		//consider only the current_date..
		$timeslot_start_date = $param_thing_timeslot["date"];
		$timeslot_start_time = $param_thing_timeslot["start_time"];
		$timeslot_end_time = $param_thing_timeslot["end_time"];
		$timeslot_duration = $param_thing_timeslot["duration"];
		
		/*echo "current_date : $current_start_date \n";
		echo "start_time : $current_start_time \n";
		echo "end_time : $current_end_time \n";
		echo "slot date : $timeslot_start_date \n";
		echo "slot start time : $timeslot_start_time \n";
		echo "slot end time : $timeslot_end_time \n";*/
		
		$in_the_slot = false;
		
		//check the timeslot start time is within the time limits
		$ret_val = funct_compare_dates(array($current_start_date, $timeslot_start_date), NULL);
	
		if($ret_val[0] == 0)
		{
			$ret_val = funct_compare_time(array($current_start_time, $timeslot_start_time), NULL);
			if($ret_val[0] <= 0)
			{
				$ret_val = funct_compare_time(array($current_end_time, $timeslot_start_time), NULL);
				if($ret_val[0] > 0)
				{
					$in_the_slot = true;
				}
			}
			
			//on the other hand..	
			//check the timeslot end time is within the time limits
			$ret_val = funct_compare_time(array($current_start_time, $timeslot_end_time), NULL);
			if($ret_val[0] < 0)
			{
				$ret_val = funct_compare_time(array($current_end_time, $timeslot_end_time), NULL);
				if($ret_val[0] >= 0)
				{
					$in_the_slot = true;
				}
			}
		}
		
		if($in_the_slot == true) //no way this time limit is appropriate..
		{
			$final_ret_val["is_free"] = false;
			$final_ret_val["new_date"] = $current_start_date;
			$final_ret_val["new_time"] = $timeslot_end_time;
			
			return $final_ret_val;
		}
	}
	$final_ret_val["is_free"] =true;
	return $final_ret_val;
}

?>